#include "Connection.h"

Connection::Connection()
{
    _conn = mysql_init(nullptr);
}

Connection::~Connection()
{
    if (_conn != nullptr)
        mysql_close(_conn);
}

bool Connection::connect(string ip, unsigned short port, string username, string passwd, string dbname)
{
    MYSQL *p = mysql_real_connect(_conn, ip.c_str(), username.c_str(), passwd.c_str(), dbname.c_str(), port, nullptr, 0);

    // if (p != nullptr)
    //     return true;
    // return false;

    return p != nullptr;
}

bool Connection::update(string sql)
{

    //执行 insert delete update 操作
    if (mysql_query(_conn, sql.c_str()))
    {
        LOG("更新失败:" + sql);
        cout << mysql_errno(_conn) << endl;
        return false;
    }
    return true;
}
